package com.week.tiny_video.controller;

import com.week.tiny_video.pojo.Concern;
import com.week.tiny_video.pojo.User;
import com.week.tiny_video.result.Result;
import com.week.tiny_video.service.ConcernService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
@CrossOrigin(allowCredentials = "true")
@RestController
public class ConcernController {


    @Autowired
    private ConcernService concernService;

    //我的关注
    @GetMapping("concern")
    public Result listByFollower(HttpSession session){
        System.out.println(session.getId());
        User user = (User) session.getAttribute("user");
        List<Concern> concerns = concernService.listByFollower(user.getId());
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",concerns);
        return new Result(200,"我的关注",true,map);
    }

    //我的粉丝
    @GetMapping("fans")
    public Result listByFans(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Concern> fans = concernService.listByFans(user.getId());
        HashMap<String,Object> map = new HashMap<>();
        map.put("list",fans);
        return new Result(200,"我的粉丝",true,map);
    }
    //添加关注
    @PostMapping("concern")
    public Result add(@RequestBody User beWatch,HttpSession session){
        User user = (User) session.getAttribute("user");
        Concern concern = new Concern();
        concern.setFollowUser(user);
        concern.setFans(beWatch);
        concernService.add(concern);
        HashMap<String,Object> map = new HashMap<>();
        return new Result(200,"新增成功",true,map);
    }

    @GetMapping("concern/{id}")
    public Result isExist(@PathVariable("id") int id,HttpSession session){
        User user = (User) session.getAttribute("user");
        int userId = user.getId();
        boolean exist = concernService.isExist(userId, id);
        if(exist){
            HashMap<String,Object> map = new HashMap<>();
            map.put("concern",concernService.getByBoth(userId,id));
            return new Result(200,"关注",true,map);
        }
        else {
            return new Result(400,"未关注",false);
        }


    }

    //取消关注
    @DeleteMapping("concern/{id}")
    public Result delete(@PathVariable("id") Integer id){
        concernService.delete(id);
        return new Result(200,"删除成功",true);

    }

}
